<template>
  <div>
    <el-container style="height: 800px; border: 1px solid #eee">
      <el-aside  width="200px" style="background-color: rgb(238, 241, 246)">
        <Aside></Aside>
      </el-aside>
      <el-container>
        <el-header style="text-align: right; font-size: 12px;">
          <Head></Head>
        </el-header>
        <el-main>
          <el-table
              :data="tableData"
              style="width: 100%">
            <el-table-column
                prop="courseId"
                label="课程ID"
                >
            </el-table-column>
            <el-table-column
                prop="courseName"
                label="课程名称"
                >
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
                width="100px">
              <template #default="scope">
                <el-button @click="showTeacher(scope.row)" type="text" size="small">查看任课教师</el-button>
                <el-button type="text" size="small" @click="confirmDelete(scope.row.courseId)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <el-dialog title="编辑" v-model="dialogFormVisible">
            <el-form :model="form">
              <!--              <el-form-item label="学生学号" :label-width="formLabelWidth">-->
              <!--                <el-input v-model="form.studentId" autocomplete="off"></el-input>-->
              <!--              </el-form-item>-->
              <el-form-item label="课程名称" :label-width="formLabelWidth">
                <el-input v-model="form.courseName" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>
            <template #footer>
                    <span class="dialog-footer">
                      <el-button @click="dialogFormVisible = false">取 消</el-button>
                      <el-button type="primary" @click="pushEditInfo(doMethod)">确 定</el-button>
                    </span>
            </template>
          </el-dialog>

          <el-dialog title="教师信息" v-model="dialogTeacherInfoVisible">
            <el-form>
              <!--              <el-form-item label="学生学号" :label-width="formLabelWidth">-->
              <!--                <el-input v-model="form.studentId" autocomplete="off"></el-input>-->
              <!--              </el-form-item>-->
              <el-table :data="teacherInfo">
                <el-table-column
                    prop="teacherId"
                    label="教职工号"
                    width="80px"/>
                <el-table-column
                    prop="teacherName"
                    label="教师姓名"
                    width="120px"/>
              </el-table>
<!--              <el-form-item label="课程名称" :label-width="formLabelWidth">-->
<!--                <el-input v-model="form.courseName" autocomplete="off"></el-input>-->
<!--              </el-form-item>-->
            </el-form>
            <template #footer>
                    <span class="dialog-footer">
                      <el-button type="primary" @click="dialogTeacherInfoVisible=false">确 定</el-button>
                    </span>
            </template>
          </el-dialog>

          <el-button class="el-button" size="medium" @click="addInfo">新增</el-button>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import Head from "@/components/Head";
import Aside from "@/components/Aside";
export default {
  components: {
    Head,
    Aside
  },
  name: "Course",
  data() {
    return {
      tableData : [],
      teacherInfo: [],
      formLabelWidth : '120px',
      dialogFormVisible: false,
      dialogTeacherInfoVisible : false,
      doMethod : '',
      form: {
        courseId : '',
        courseName: ''
      }
    }
  },
  created() {
    this.getCourseList()
  },
  methods: {
    getCourseList() {
      const _this = this
      _this.axios.get("/course/list").then(res => {
        console.log(res.data.data)
        this.tableData = res.data.data
      })
    },
    showTeacher(row) {
      const _this = this
      this.axios.get("/course/getTeacherList/" + row.courseId).then(res => {
        console.log(res)
        _this.teacherInfo = res.data.data
        _this.dialogTeacherInfoVisible = true
      })
    },
    confirmDelete(id) {
      this.$confirm('此操作将永久删除该记录,并使得该节课任课老师当场改行,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const _this = this;
        this.axios.post("/course/del/" + id).then(res => {
          console.log(res)
          _this.getCourseList()
        })
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    pushEditInfo(action) {
      const _this = this;
      this.axios.post("/course/" + action, this.form).then(res => {
        console.log(res)
        _this.dialogFormVisible = false
        _this.getCourseList()
      })
    },
    addInfo() {
      this.form.courseId = ''
      this.form.courseName = ''
      this.dialogFormVisible = true
      this.doMethod = 'add'
    }
    }
}
</script>

<style scoped>

</style>
